package com.hrm.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.hrm.domain.JSONNoticeExtResult;
import com.hrm.domain.Notice;
import com.hrm.domain.NoticeExt;
import com.hrm.domain.PageHelper;
import com.hrm.domain.User;
import com.hrm.service.NoticeService;
import com.hrm.service.UserService;
import com.hrm.service.Impl.NoticeServiceImpl;
import com.hrm.service.Impl.UserServiceImpl;

/*
 * 公告数据控制层
 */
@WebServlet(urlPatterns = { "/getNotices", "/addNotice", "/delNotice", "/delNotices", "/modifyNotice" })
@SuppressWarnings("all") // 使用注解抑制警告信息
public class NoticeController extends HttpServlet {

	// 定义全局变量
	private NoticeService ns = new NoticeServiceImpl(); // CURD公告数据

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String url = request.getRequestURI(); // 获取跳转路径
		// 进行截取并判断
		String operator = url.substring(url.lastIndexOf("/") + 1);
		if ("getNotices".equals(operator)) { // 分页查询 + 模糊
			getNotices(request, response);
		} else if ("addNotice".equals(operator)) { // 发布公告
			addNotice(request, response);
		} else if ("delNotice".equals(operator)) { // 删除指定公告
			delNotice(request, response);
		} else if ("delNotices".equals(operator)) { // 删除多个公告
			delNotices(request, response);
		} else if ("modifyNotice".equals(operator)) { // 修改指定公告的信息
			modifyNotice(request, response);
		}
	}

	// 分页查询 + 模糊
	private void getNotices(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PageHelper ph = new PageHelper(); // 简化分页查询
		List<NoticeExt> notices = new ArrayList<>(); // 用于存储查询到的数据
		int count = 0; // 用于记录数据条数
		int flag = 0; // 用于判断情况
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String ui = request.getParameter("userid");
		int userID = Integer.parseInt(ui.replaceAll(" ", ""));
		// 获取用户集合
		UserService us = new UserServiceImpl();
		List<User> users = us.getAllUser();
		// 获取查询出的数据记录数,并设置模糊查询
		if ("".equals(title) && "".equals(content) && userID == 0) { // 查询条件没有输入时
			flag = 1;
			count = ns.getCount(flag);
		} else if (userID != 0) { // 输入了查询条件 包括用户ID
			flag = 2;
			title = "%" + title + "%";
			content = "%" + content + "%";
			count = ns.getCount(flag, title, content, userID);
		} else { // 输入了查询条件 不包括用户ID
			flag = 3;
			title = "%" + title + "%";
			content = "%" + content + "%";
			count = ns.getCount(flag, title, content);
		}
		ph.setPageIndex(1); // 设置当前页的初始数据
		// 获取当前页
		String pageNo = request.getParameter("pageNo");
		if (pageNo != null) { // 说明点了页码
			ph.setPageIndex(Integer.parseInt(pageNo)); // 重新设置页码
			ph.setTotalPageSum(ph.getTotalPageSum()); // 重新设置总页数
		}
		ph.setPageSize(ph.getPageSize()); // 设置每页显示的记录条数
		ph.setTotalRecordSum(count); // 设置总记录条数
		// 获取当前页的数据
		if (flag == 1) { // 查询条件没有输入时
			notices = ns.getNoticeExtsByTerm(flag, ph.getStartRowNum(), ph.getPageSize());
		} else if (flag == 2) { // 输入了查询条件 包括用户ID
			notices = ns.getNoticeExtsByTerm(flag, title, content, userID, ph.getStartRowNum(), ph.getPageSize());
		} else if (flag == 2) { // 输入了查询条件 不包括用户ID
			notices = ns.getNoticeExtsByTerm(flag, title, content, ph.getStartRowNum(), ph.getPageSize());
		}
		ph.setTotalPageSum(ph.getTotalPageSum()); // 设置总页数
		// 将查询到的数据转化为json
		JSONNoticeExtResult aur = new JSONNoticeExtResult();
		aur.setList(notices);
		aur.setPh(ph);
		if (notices != null && notices.size() > 0) {
			aur.setMsg("success");
			aur.setUsers(users);
		}
		// 把aur对象转换成json数据
		String JobsJson = JSON.toJSONString(aur);
		// 把结果响应给客户端
		PrintWriter out = response.getWriter();
		out.write(JobsJson);
		out.flush();
		out.close();
	}

	// 发布公告
	private void addNotice(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 获取前台传递的参数
		String title = request.getParameter("ntitle");
		String content = request.getParameter("nconent");
		// 获取当前用户对象
		User user = JSON.parseObject((String) request.getSession().getAttribute("user"), User.class);
		String msg = ""; // 定义返回消息
		// 多种判断,防止异常
		if (title != "" && content != "") {
			// 进行发布
			Notice notice = new Notice(title, content, user.getuID());
			ns.addNotice(notice);
			msg = "success";
		} else {
			msg = "fail"; // 存在空数据
		}
		// 将返回信息封装
		JSONNoticeExtResult aur = new JSONNoticeExtResult();
		aur.setMsg(msg);
		// 把aur对象转换成json数据
		String mJSON = JSON.toJSONString(aur);
		// 把结果响应给客户端
		PrintWriter out = response.getWriter();
		out.write(mJSON);
		out.flush();
		out.close();
	}

	// 删除公告
	private void delNotice(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int noticeID = Integer.parseInt(request.getParameter("nid"));
		String msg = "success"; // 定义返回信息
		// 删除公告
		ns.delNotice(noticeID);
		// 将返回信息封装
		JSONNoticeExtResult aur = new JSONNoticeExtResult();
		aur.setMsg(msg);
		// 把aur对象转换成json数据
		String mJSON = JSON.toJSONString(aur);
		// 把结果响应给客户端
		PrintWriter out = response.getWriter();
		out.write(mJSON);
		out.flush();
		out.close();
	}

	// 删除多个公告
	private void delNotices(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 获取前台传过来的数据
		String noticesJSON = request.getParameter("notices");
		// 将JSON数组转换为集合
		List<String> notices = JSON.parseArray(noticesJSON, String.class);
		String msg = "success"; // 定义返回信息
		// 循环判断
		for (int i = 0; i < notices.size(); i++) {
			String noticeID = notices.get(i).replaceAll(" ", "");
			// 删除公告
			ns.delNotice(Integer.parseInt(noticeID));
		}
		// 将返回信息封装
		JSONNoticeExtResult aur = new JSONNoticeExtResult();
		aur.setMsg(msg);
		// 把aur对象转换成json数据
		String mJSON = JSON.toJSONString(aur);
		// 把结果响应给客户端
		PrintWriter out = response.getWriter();
		out.write(mJSON);
		out.flush();
		out.close();
	}

	// 修改指定公告的信息
	private void modifyNotice(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 获取前台传递的参数
		int noticeID = Integer.parseInt(request.getParameter("id"));
		String newTitle = request.getParameter("ntitle");
		String newContent = request.getParameter("ncontent");
		String msg = ""; // 定义返回消息
		Notice notice = ns.getNoticeByID(noticeID);
		// 多种判断,防止异常
		if (newTitle != "" && newContent != "") {
			// 进行修改
			Notice newNotice = new Notice(newTitle, newContent, notice.getnUserID());
			ns.modifyNoticeByID(newNotice, noticeID);
			msg = "success";
		} else {
			msg = "fail"; // 存在空数据
		}
		// 将返回信息封装
		JSONNoticeExtResult aur = new JSONNoticeExtResult();
		aur.setMsg(msg);
		// 把aur对象转换成json数据
		String mJSON = JSON.toJSONString(aur);
		// 把结果响应给客户端
		PrintWriter out = response.getWriter();
		out.write(mJSON);
		out.flush();
		out.close();
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
